import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

class SearchTopView extends StatefulWidget {
  final ValueChanged<String>? onSearch;
  final VoidCallback? onBack;

  const SearchTopView({Key? key, this.onSearch, this.onBack}) : super(key: key);

  @override
  _SearchTopViewState createState() => _SearchTopViewState();
}

class _SearchTopViewState extends State<SearchTopView> {
  final TextEditingController _controller = TextEditingController();

  void _handleSearch() {
    if (_controller.text.trim().isNotEmpty) {
      widget.onSearch?.call(_controller.text.trim());
    } else {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(AppLocalizations.of(context)!.notempty)),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    final localizations = AppLocalizations.of(context)!;

    return Container(
      alignment: Alignment.center,
      color: Color(0xFFF8F6F7),
      padding: const EdgeInsets.only(left: 0, right: 0, top: 50, bottom: 10),
      child: Row(
       children: [
          IconButton(
            icon: Image.asset('assets/images/leftpush.png',width: 20, height: 20),
            onPressed: widget.onBack,
          ),
          Expanded(
            child: Container(
              height: 40,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20),
              ),
              padding: const EdgeInsets.symmetric(horizontal: 15),
              child: Row(
                children: [
                  Image.asset("assets/images/serach2.png", width: 16, height: 16, fit: BoxFit.scaleDown),
                  const SizedBox(width: 10),
                  Expanded(
                    child: Container(
                      alignment: Alignment.center,
                      child: TextField(
                        controller: _controller,
                        style: TextStyle(color: Color(0xFF505050), fontSize: 15),
                        decoration: InputDecoration(
                          hintText: localizations.pleaseEnterProduct,
                          hintStyle: TextStyle(color: Color(0xFFACACAC), fontSize: 15),
                          border: InputBorder.none,
                          isDense: true,
                          contentPadding: EdgeInsets.zero,
                        ),
                        onSubmitted: (value) => _handleSearch(),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          const SizedBox(width: 10),
          SizedBox(            
            width: 68,
            height: 36,  
            child: ElevatedButton(            
              onPressed: _handleSearch,
              style: ElevatedButton.styleFrom(
                padding: EdgeInsets.symmetric(horizontal: 6),
                // 设置按钮的背景颜色为 #5D5FEE
                backgroundColor: const Color(0xFF5D5FEE), 
                // 设置按钮的文本颜色为白色
                foregroundColor: Colors.white, 
                
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(18),
                ),
              ),
              child: Text(AppLocalizations.of(context)!.search, style: TextStyle(fontSize: 14)),
            ),
          ),
          const SizedBox(width: 15)
        ],
      ),
    );
  }
}
